<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户昵称" prop="memberNick">
        <el-input
          v-model="queryParams.memberNick"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否结算" prop="isSettle">
        <el-select v-model="queryParams.isSettle" placeholder="请选择是否结算" clearable>
          <el-option
            v-for="dict in dict.type.sl_deduction_settle"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="deductionList" @selection-change="handleSelectionChange">
      <el-table-column label="用户" align="center" prop="memberNick" />
      <el-table-column label="金额" align="center" prop="dmAmount" />
      <el-table-column label="是否结算" align="center" prop="isSettle">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_deduction_settle" :value="scope.row.isSettle"/>
        </template>
      </el-table-column>
      <el-table-column label="扣款原因" align="center" prop="dmReason" />
      <el-table-column label="扣款时间" align="center" prop="dmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dmTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sl:deduction:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sl:deduction:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改扣款记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="扣款单ID" prop="dmId">
          <el-input v-model="form.dmId" placeholder="请输入扣款单ID" />
        </el-form-item>
        <el-form-item label="报工单ID" prop="rfworkId">
          <el-input v-model="form.rfworkId" placeholder="请输入报工单ID" />
        </el-form-item>
        <el-form-item label="报工项ID" prop="rworkId">
          <el-input v-model="form.rworkId" placeholder="请输入报工项ID" />
        </el-form-item>
        <el-form-item label="用户" prop="memberUser">
          <el-input v-model="form.memberUser" placeholder="请输入用户" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="memberNick">
          <el-input v-model="form.memberNick" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="金额" prop="dmAmount">
          <el-input v-model="form.dmAmount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="是否结算0已结算1未结算" prop="isSettle">
          <el-radio-group v-model="form.isSettle">
            <el-radio
              v-for="dict in dict.type.sl_deduction_settle"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="扣款原因" prop="dmReason">
          <el-input v-model="form.dmReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="扣款时间" prop="dmTime">
          <el-date-picker clearable
            v-model="form.dmTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择扣款时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeduction, getDeduction, delDeduction, addDeduction, updateDeduction } from "@/api/sl/deduction";

export default {
  name: "Deduction",
  dicts: ['sl_deduction_settle'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 扣款记录表格数据
      deductionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dmId: null,
        rfworkId: null,
        rworkId: null,
        memberUser: null,
        memberNick: null,
        dmAmount: null,
        isSettle: null,
        dmReason: null,
        dmTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询扣款记录列表 */
    getList() {
      this.loading = true;
      listDeduction(this.queryParams).then(response => {
        this.deductionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deductionId: null,
        dmId: null,
        rfworkId: null,
        rworkId: null,
        memberUser: null,
        memberNick: null,
        dmAmount: null,
        isSettle: null,
        dmReason: null,
        dmTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deductionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加扣款记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deductionId = row.deductionId || this.ids
      getDeduction(deductionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改扣款记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deductionId != null) {
            updateDeduction(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeduction(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deductionIds = row.deductionId || this.ids;
      this.$modal.confirm('是否确认删除扣款记录编号为"' + deductionIds + '"的数据项？').then(function() {
        return delDeduction(deductionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sl/deduction/export', {
        ...this.queryParams
      }, `deduction_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
